package com.eatme.web.security.rolehierarchy;

import com.eatme.web.security.dao.SecurityRoleDao;
import com.eatme.web.security.domain.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;

import static com.eatme.web.util.Constants.ROLE_ADMIN;
import static com.eatme.web.util.Constants.ROLE_STAFF;
@Component
public class RoleHierarchyService {

    @Autowired
    SecurityRoleDao securityRoleDao;

    public String getRoleHierarchyExpr() {
        // List<Role> roles = securityRoleDao.findAll();
        Set<Role> roles = securityRoleDao.findAll().stream().collect(Collectors.toSet());
        return roles.stream()
            .flatMap(role -> role.getPermissions().stream()
                .map(permission -> role.getName() + " > " + permission.getAuthority() + "\n"))
            .collect(Collectors.joining("", ROLE_ADMIN + " > " + ROLE_STAFF, "\n"));
    }
}
